package domain;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import center.BaseDomain;

public class Alternative extends BaseDomain{
	
	private long id;
	private String name;
	private double priority;
	
	private Project project;
	
	public double getPriority() {
		return priority;
	}
	public void setPriority(double priority) {
		this.priority = priority;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Project getProject() {
		return project;
	}
	public void setProject(Project project) {
		this.project = project;
	}
	
	public void insert() throws SQLException{
		Connection conn = getConnection();
		PreparedStatement ps = null;
		 StringBuffer sql = new StringBuffer();
			 sql.append("insert into alternatives(name, project_id, priority) ");
			 sql.append(" values(?, ?, ?)");
		 int index =1;
		 ps = conn.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS);
		 	ps.setString(index++, this.name);
		 	ps.setLong(index++, this.project.getId());
		 	ps.setLong(index++, 0);
		 ps.executeUpdate();
		 
		 ResultSet resultSet = ps.getGeneratedKeys();
			if (resultSet.next()) {
				long generatedid = resultSet.getLong(1);
				ps.close();
				conn.close();
				this.id = generatedid;
			} else {
				 System.out.println("Cannot Get Genereated ID!!!!");
			}
	}
	
	public static List<Alternative> getAlternativesOf(long projectId) throws SQLException {
		Connection conn = getConnection();
		PreparedStatement ps = null;
		 StringBuffer sql = new StringBuffer();
			 sql.append("select id, name, priority from alternatives where project_id = ?");
		 ps = conn.prepareStatement(sql.toString());
		 int index=1;
		 	ps.setLong(index++, projectId);
		 	
		 ResultSet rs = ps.executeQuery();
		 
		 List<Alternative> alternatives = new ArrayList<Alternative>();
		 while(rs.next()){
			Alternative alternative = new Alternative();
				alternative.setName(rs.getString("name"));
				alternative.setId(rs.getLong("id"));
				alternative.setPriority(rs.getDouble("priority"));
				 
			alternatives.add(alternative);
		 }
		 ps.close();
		 conn.close();
		 
		 return alternatives;
	}
	
	public static Alternative find(long id) throws SQLException {
		Connection conn = getConnection();
		PreparedStatement ps = null;
		 StringBuffer sql = new StringBuffer();
			 sql.append("select id, name, priority from alternatives where id = ? ");
		int index=1;
		 ps = conn.prepareStatement(sql.toString());
		 	ps.setLong(index++, id);
		 ResultSet rs = ps.executeQuery();
		 
		 Alternative a = new Alternative();
		 while(rs.next()){
			 a.setName(rs.getString("name"));
			 a.setId(rs.getLong("id"));
			 a.setPriority(rs.getDouble("priority"));
		 }
		 ps.close();
		 conn.close();
		 
		 return a;
	}
	
	public void update() throws SQLException{
		Connection conn = getConnection();
		PreparedStatement ps = null;
		 StringBuffer sql = new StringBuffer();
			 sql.append("update alternatives set priority = ? where id = ? ");
		 int index =1;
		 ps = conn.prepareStatement(sql.toString());
		 	ps.setDouble(index++, this.priority);
		 	ps.setLong(index++, this.id);
		 ps.executeUpdate();
		 
		ps.close();
		conn.close();
	}
}
